// pages/company/company.js
const app = getApp()
Page({
  data: {
    region: ['省，市，区'],
    items: [{
        value: '1',
        name: '是',
        checked: ''
      },
      {
        value: '0',
        name: '否',
        checked: ''
      },
    ],
    checked: false,
    company: '',
    name: '',
    tel: '',
    num: '',
    email: '',
    model:false
  },

  onLoad: function(options) {
    this.setData({
      userlogin: wx.getStorageSync('token'),
      tellogin: wx.getStorageSync('tel'),
    })
  },

  onShow: function() {

  },
  telChange(e) { //手机号码
    this.data.tel = e.detail.value;
  },
  companyChange(e) { //企业名字
    console.log(e.detail.value)
    this.data.company = e.detail.value;
  },
  nameChange(e) { //数量
    this.data.num = e.detail.value;
  },
  nameChange(e) { //对接人
    this.data.name = e.detail.value;
  },
  bindtap1: function(e) { //协议选中
    this.setData({
      checked: !this.data.checked
    })
  },
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let region = e.detail.value;
    if (e.detail.value) {
      this.setData({
        region: region
      })
      console.log(this.data.region)
    }
  },
  numChange: function(e) { //协议选中
    this.data.num = e.detail.value;
  },
  detail: function(e) { //查看说明协议
    wx.navigateTo({
      url: '/pages/persondz/detailexplain/detailexpalin?type=company'　　 // 协议页面
    })
  },
  radioChange(e) { //选择是否二次销售
    var that = this;
    var radioValue = e.detail.value;
    this.setData({
      agin: radioValue
    })
  },
  submit() { //开始订
    if (this.check()) {
      let that = this;
      wx.showLoading()
      wx.request({ //传后台
        url: app.globalData.url + '/jxb/eorder/addCyorder',
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          token: wx.getStorageSync('token'),
          uid: wx.getStorageSync('uid'),
          cyname: this.data.company,
          secondsall: this.data.agin,
          contact: this.data.name ,
          tel: this.data.tel,
          num: this.data.num,
          address: this.data.region.join(' '),
        },
        success: function(res) {
        if(res.data.code==0){
          that.setData({
            model: true
          })
          wx.hideLoading();
        } else if (res.data.code == 405) {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '帐号异常，已被封禁，请联系客服处理',
            showCancel: false,
            cancelText: '',
            cancelColor: '',
            confirmText: '确定',
            confirmColor: '#2AA7F9',
            success: function (res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            },

          })
        }else{
           wx:wx.showToast({
             title: res.data.msg,
             icon: 'none',
           })
          wx.hideLoading(); 
         }
        },

      })
    }
  },
  suer() { //支付成功跳转
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/order/isComment',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function (res) {
        let pstatus = res.data.data.pstatus;
        console.log(pstatus)
        if (pstatus == '1') { //已评价
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else {
          wx.navigateTo({
            url: '/pages/ratestart/ratestart',
          })
        }
      },

    })
  },
  login: function() { //登录
    wx.showLoading();
    let that = this;
    wx.login({
      success: res => {
        if (res.code) { //有code
          app.globalData.code = res.code;
          wx.getSetting({
            success: function(res) { //已授权
              if (res.authSetting['scope.userInfo']) {
                wx.getUserInfo({ //调用用户信息
                  success: res => { //获取信息   
                    wx.setStorageSync('headurl', res.userInfo.avatarUrl);
                    wx.setStorageSync('username', res.userInfo.nickName);
                    wx.request({ //传后台
                      url: app.globalData.url + '/jxb/login/login/loginAuth',
                      method: 'POST',
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      data: {
                        code: app.globalData.code,
                        encryptedData: res.encryptedData,
                        iv: res.iv
                      },
                      success: function(res3) {
                        wx.hideLoading();
                        wx.setStorageSync('token', res3.data.data.token);
                        wx.setStorageSync('uid', res3.data.data.uid);
                        app.globalData.isLogin = true;
                        that.setData({
                          userlogin: true,
                        })
                        wx.showToast({
                          title: '已登录',
                          duration: 2000,
                          icon: 'none',
                        })
                      },

                    })
                  }
                })
              } else { //未授权

                // that.getUserInfo();
              }
            }

          }); // 查看授权

        } else { //没有code

        }
      }
    })
  },
  getUserInfo(res) { //授权
    console.log(res.detail.userInfo)
    if (res.detail.userInfo) {
      console.log("点击了同意授权");
      this.login();
    } else {
      console.log("点击了拒绝授权");
    }
  },
  getPhoneNumber(e) {
    var data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      token: wx.getStorageSync('token'),
    }
    let taht = this;
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      wx.request({
        method: "post",
        url: app.globalData.url + '/jxb/login/login/getPhoneNumber',
        data: data,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          console.log(res.data)
          wx.setStorageSync('tel', res.data.data.tel)
          taht.setData({
            tellogin: true,
            // tel: res.data.data.tel,
          })
          taht.saveinfo('',res.data.data.tel,'')
          var phoneNum = res.data;
          wx.hideLoading()
          wx.setStorage({
            key: 'tel',
            data: res.data.data.tel,
          })
          wx.showToast({
            title: '授权成功',
            icon: 'success',
            duration: 2000
          })
        },
        fail: function(res) {
          console.log(res)
        }
      })

    }

  },
  saveinfo(remark, tel, avatar) { //保存信息
    wx.request({
      url: app.globalData.url + '/jxb/oneself/updateUserInfo',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
        remark: remark,
        tel: tel,
        avatar: avatar,
      },
      success: function (res) {
        // wx.showToast({
        //   title: '修改成功',
        //   icon: 'success',
        //   duration: 2000
        // })
      },

    })
  },
  check() {
    console.log(this.data.agin)
    if (this.data.checked == true) {
      wx.showToast({
        title: '请同意并阅读《江小白企业订制特别说明》',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.company == '') {
      wx.showToast({
        title: '请填写终端名称',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (!this.data.agin) {
      wx.showToast({
        title: '请选择是否二次销售',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.name == '') {
      wx.showToast({
        title: '请填写对接人',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.tel == '') {
      wx.showToast({
        title: '请填写您的手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else {
      if (!(/^1[345789]\d{9}$/.test(this.data.tel))) {
        wx.showToast({
          title: '手机格式有误',
          icon: 'none',
          duration: 2000
        });
        return false;
      } else {}
    }
    if (this.data.region[0] == '省，市，区') {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.num == '') {
      wx.showToast({
        title: '请输入数量',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (this.data.num < 200) {
      wx.showToast({
        title: '起订数量为200',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    return true
  }

})